import { getSendCode,reqUserRegister,reqUserLogin } from "@/api";
//user的VUEX模块
//VUE中的四个核心概念
const state = {
    //存储数据
   uesrTempId:'',
   code:'',
   token:'',
};
const mutations = {
    //直接修改数据
    RECEIVE_SETCODE(state,code){
        state.code = code;
    },
    RECEIVE_SETTOKEN(state,token){
        state.token = token;
    },
};
const actions = {
    //与组件当中的用户对接
    //一般是异步发送请求
    //提交mutations
    async getCode({commit},phone){
        const result = await getSendCode(phone);
        if(result.code === 200){
            commit("RECEIVE_SETCODE",result.data);
        }else{
            return Promise.reject(new Error('failed'));
        }
    },
    async reqUserRegister1({commit},userInfo) {
        const result = await reqUserRegister(userInfo);
        if(result.code === 200) {
            return 'ok'
        }else{
            return Promise.reject(new Error('failed'));
        }
    },
    async reqUserLogin({commit},userInfo){
        const result = await reqUserLogin(userInfo);
        if(result.code === 200){
            commit("RECEIVE_SETTOKEN",result.data);
            alert("登录成功");
        }
    }
};
const getters = {
    //后面用来简化数据的
}

export default{
    state,
    mutations,
    actions,
    getters,
};